/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ObjectListEditor.java
 *
 * Created on 29.12.2010, 09:14:01
 */

package editor;

import javax.swing.table.*;

/**
 *
 * @author Philipp
 */
public class ObjectListEditor extends javax.swing.JFrame {

    static DefaultTableModel ObjectListTableModel = new DefaultTableModel(
        null,
        new String[]{
            "Object Name","Char"
        }
    );

    /** Creates new form ObjectListEditor */
    public ObjectListEditor() {
        initComponents();
        ObjectListTable.setModel(ObjectListTableModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    public static void updateTable(){

        ObjectListTableModel.setRowCount(Toolbox.objectChooser.objectList.length);

        for(int i = 0; i < Toolbox.objectChooser.objectList.length; i++){
            ObjectListTableModel.setValueAt(Toolbox.objectChooser.objectList[i].name, i, 0);
            ObjectListTableModel.setValueAt(Toolbox.objectChooser.objectList[i].objectChar, i, 1);
        }

    }

    public void updateObjectList(){

        GameObject[] newObjectList = new GameObject[ObjectListTableModel.getRowCount()];

        for(int i = 0; i < newObjectList.length; i++){
            ObjectListTableModel.setValueAt(ObjectListTableModel.getValueAt(i, 1).toString().charAt(0), i, 1);
            newObjectList[i] = new GameObject(ObjectListTableModel.getValueAt(i, 0).toString(), ObjectListTableModel.getValueAt(i, 1).toString().charAt(0));
        }

        Toolbox.objectChooser.objectList = newObjectList;

        try{
            Toolbox.objectChooser.updateObjectChooser();
        }
        catch(Exception e){
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        ObjectListTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Object List Editor");
        setAlwaysOnTop(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        ObjectListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Object Class Name", "Char"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        ObjectListTable.setName("ObjectListTable"); // NOI18N
        ObjectListTable.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                ObjectListTablePropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(ObjectListTable);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editor/resources/list-add.png"))); // NOI18N
        jButton1.setText("Add Object");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editor/resources/list-remove.png"))); // NOI18N
        jButton2.setText("Remove Object");
        jButton2.setToolTipText("removes the selected Object from the list");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 182, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                .add(0, 0, 0)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        char randomChar = (char)(Math.random()*255);
        boolean newChar = false;

        while(newChar == false){
            newChar = true;
            randomChar = (char)(Math.random()*255);
            for(int i = 0; i < Toolbox.objectChooser.objectList.length; i++){
                if(Toolbox.objectChooser.objectList[i].objectChar == randomChar || ((int)randomChar >= 48 && (int)randomChar <= 57 )){
                    newChar = false;
                }
            }
        }

        ObjectListTableModel.addRow(new String[] { "ObjectName", randomChar+"" });

        updateObjectList();

    }//GEN-LAST:event_jButton1ActionPerformed

    private void ObjectListTablePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_ObjectListTablePropertyChange

        GameObject[] newObjectList = new GameObject[ObjectListTableModel.getRowCount()];

        for(int i = 0; i < newObjectList.length; i++){
            ObjectListTableModel.setValueAt(ObjectListTableModel.getValueAt(i, 1).toString().charAt(0), i, 1);
            newObjectList[i] = new GameObject(ObjectListTableModel.getValueAt(i, 0).toString(), ObjectListTableModel.getValueAt(i, 1).toString().charAt(0));
        }

        Toolbox.objectChooser.objectList = newObjectList;

        try{
            Toolbox.objectChooser.updateObjectChooser();
        }
        catch(Exception e){
        }

    }//GEN-LAST:event_ObjectListTablePropertyChange

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        for(int i = ObjectListTable.getSelectedRow(); i < ObjectListTableModel.getRowCount() - 1; i++){
            ObjectListTableModel.setValueAt(ObjectListTableModel.getValueAt(i+1, 0), i, 0);
            ObjectListTableModel.setValueAt(ObjectListTableModel.getValueAt(i+1, 1), i, 1);
        }

        ObjectListTableModel.setRowCount(ObjectListTableModel.getRowCount()-1);

    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JTable ObjectListTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
